package com.bfly.mall.wechat.mp.service.message.event;

import com.alibaba.fastjson.JSONObject;
import com.bfly.common.json.JsonUtil;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.mall.user.entity.User;
import com.bfly.mall.wechat.entity.WeChatMp;
import com.bfly.mall.wechat.entity.WeChatMpMaterial;
import com.bfly.mall.wechat.entity.WeChatMpMenu;
import com.bfly.mall.wechat.enums.WeChatMpMenuCategoryEnum;
import com.bfly.mall.wechat.mp.dto.WeChatMpMsgResponse;
import com.bfly.mall.wechat.service.IWeChatMpMaterialService;
import com.bfly.mall.wechat.service.IWeChatMpMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 点击菜单事件处理
 *
 * @author 长沙商脉科技有限公司
 * @date 2021/1/7 22:34
 */
@Component
public class ClickEventMenuHandler {

    private Logger logger = LoggerFactory.getLogger(ClickEventMenuHandler.class);
    @Autowired
    private IWeChatMpMenuService weChatMpMenuService;
    @Autowired
    private IWeChatMpMaterialService weChatMpMaterialService;

    /**
     * 点击事件的Key是构成:固定前缀+ 菜单ID
     * 固定前缀参见: 例如 key:send_user_voice_10 表示id为10的菜单时发送语音的菜单按钮
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/1/7 22:43
     * @see com.bfly.mall.wechat.enums.WeChatMpMenuCategoryEnum
     */
    public String execute(String key, WeChatMp mp, User user) {
        String items[] = key.split("_");
        if (items.length == 0) {
            logger.warn(String.format("菜单Key:%s 解析错误!", key));
            return "";
        }
        int menuId = DataConvertUtils.convertToInteger(items[items.length - 1]);

        WeChatMpMenu menu = weChatMpMenuService.get(menuId);
        if (menu == null) {
            logger.warn(String.format("不存在的菜单ID: %d", menuId));
            return "";
        }
        JSONObject json = JsonUtil.parse(menu.getData(), JSONObject.class);

        WeChatMpMenuCategoryEnum categoryEnum = WeChatMpMenuCategoryEnum.getCategory(menu.getCategory());
        WeChatMpMsgResponse response = new WeChatMpMsgResponse();
        response.setToUserName(user.getOpenId());
        response.setFromUserName(mp.getOriginId());
        response.setCreateTime(System.currentTimeMillis() / 1000);
        String xml = "";
        switch (categoryEnum) {
            case SEND_TEXT_MENU:
                response.setContent(json.getString("content"));
                xml = response.buildText().build();
                break;
            case SEND_PIC_MENU:
                response.setMediaId(json.getString("pictureMedia"));
                xml = response.buildPic().build();
                break;
            case SEND_VOICE_MENU:
                response.setMediaId(json.getString("voiceMedia"));
                xml = response.buildVoice().build();
                break;
            case SEND_VIDEO_MENU:
                WeChatMpMaterial videoMedia = weChatMpMaterialService.getMaterial(menu.getPlatformId(), json.getString("videoMedia"));
                response.setMediaId(json.getString("videoMedia"));
                response.setTitle(json.getString(videoMedia.getTitle()));
                response.setDescription(videoMedia.getDigest());
                xml = response.buildVideo().build();
                break;
            case SEND_NEWS_MENU:
                List<WeChatMpMaterial> newsMedia = weChatMpMaterialService.getNewsMaterial(menu.getPlatformId(), json.getString("newsMedia"));
                xml = response.buildNews(newsMedia).build();
                break;
        }
        return xml;
    }
}
